package cc.rengu.igas.cops.core.service.trans.asyncall;

import cc.rengu.igas.cops.common.constant.CopsParamConstant;
import cc.rengu.igas.cops.common.constant.CopsTreeNodeConstant;
import cc.rengu.igas.momp.facade.request.ComboPaymentReversalRequest;
import cc.rengu.igas.momp.facade.response.ComboPaymentReversalResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.enums.BizStatusEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * 营销合并支付异步撤销
 */
public class CopsAsynComboPaymentCancelService extends DubboAsynBaseService {

    @BIZ_PROCESS
    public void McpsAsynComboPaymentCancelService() throws Exception {
        OrderService orderService = new OrderServiceImpl();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        PayOrderInfo payOrderInfo = (PayOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.MOMP_CANCEL_ORDER_INFO, PayOrderInfo.class);
        if (null == payOrderInfo) {
            rglog.debug("获取营销撤销订单表异常");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        }
        ComboPaymentReversalRequest request = (ComboPaymentReversalRequest) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_REQUEST, ComboPaymentReversalRequest.class);
        if (null == request) {
            rglog.debug("请求报文异常");
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        Result<ComboPaymentReversalResponse> comboPaymentReversalResponseResult = (Result<ComboPaymentReversalResponse>) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_RESPONSE, Result.class);
        if (null == comboPaymentReversalResponseResult) {
            rglog.debug("请求报文异常");
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ComboPaymentReversalResponse response = (ComboPaymentReversalResponse) JSONObject.parseObject(JSONObject.toJSONString(comboPaymentReversalResponseResult.getResult()), ComboPaymentReversalResponse.class);
        if (null == response) {
            rglog.debug("请求报文异常");
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }

        /*判断冲正结果，更新状态*/
        if (comboPaymentReversalResponseResult.isSuccess() && CopsParamConstant.DUBBO_SUCCESS.equals(response.getRespCode())) {
            payOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
            payOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
            payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            payOrderInfo.setRespDesc(response.getRespDesc());
            payOrderInfo.setRespCode(response.getRespCode());
            /*获取原营销订单*/
            PayOrderInfo comboPaymentPayOrderInfo = (PayOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.MOMP_COMPAY_ORDER_INFO, PayOrderInfo.class);
            if (null == comboPaymentPayOrderInfo) {
                rglog.error("获取营销订单表异常");
                throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
            }
            /*更新原订单状态*/
            comboPaymentPayOrderInfo.setBizStatus(BizStatusEnum.REVSAL.getStatus());
            if (orderService.updateOrderInfo(null, null, comboPaymentPayOrderInfo)) {
                rglog.info("更新营销支付订单信息成功");
            } else {
                rglog.error("更新营销支付订单信息失败");
            }
        } else if (comboPaymentReversalResponseResult.isSuccess() && OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode().equals(response.getRespCode())) {
            /*失败更新状态*/
            /*设置虚拟账户支付订单的状态*/
            payOrderInfo.setTransStatus(TransStatusEnum.TRANS_TIMEOUT.getStatus());
            payOrderInfo.setRespDesc(response.getRespDesc());
            payOrderInfo.setRespCode(response.getRespCode());
        } else {
            payOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            payOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
            payOrderInfo.setRespDesc(response.getRespDesc());
            payOrderInfo.setRespCode(response.getRespCode());
        }
        if (orderService.updateOrderInfo(null, null, payOrderInfo)) {
            rglog.info("更新营销撤销支付订单信息成功");
        } else {
            rglog.error("更新营销撤销支付订单信息失败");
        }
    }
}
